/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.handler;

/**
 *
 * @author ST
 */
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import layoutitems.ImageElement;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class LayoutParser {

    /////////////////////////////////////
    //          FIELDS
    /////////////////////////////////////
    private ArrayList<ImageElement> imageElements;
    private File xmlFile;
    /////////////////////////////////////
    //          CONSTRUCTOR
    /////////////////////////////////////
    public LayoutParser(String filepath){
        this.xmlFile = new File (filepath);
    }
    /////////////////////////////////////
    //          INNER METHODS
    /////////////////////////////////////
    public ArrayList<ImageElement> getLayoutElementsXmlParsed() {
        if (xmlFile.exists()) {
        	System.out.println("xmlFile exists");
            try {
                SAXParserFactory factory = SAXParserFactory.newInstance();
                // create a parser
                SAXParser parser = factory.newSAXParser();
                // create the reader (scanner)
                XMLReader xmlreader = parser.getXMLReader();
                // instantiate our handler
                LayoutSaxHandler laySaxHandler = new LayoutSaxHandler();
                // assign our handler
                xmlreader.setContentHandler(laySaxHandler);
                // get our data via the url class
                FileInputStream xmlInput = new FileInputStream(xmlFile);
                //InputSource is = new InputSource(fileSystemKml.openStream());
                InputSource is = new InputSource(xmlInput);
                // perform the synchronous parse           
                xmlreader.parse(is);
                // get the results - should be a fully populated RSSFeed instance, or null on error
                LayoutDataSet ds = laySaxHandler.getParsedData();
                imageElements = ds.getImageElements();
                
                
                        
            } catch (Exception e) {
                System.out.println(this.getClass().toString() + "; Exeption parsing xml-File:"+ xmlFile.getAbsolutePath());
            }
        
        }
        return imageElements;
    }
}
